package com.reporting.yb.module.system.controller.admin.reportingfile;

import com.reporting.yb.framework.apilog.core.annotation.ApiAccessLog;
import com.reporting.yb.framework.common.pojo.CommonResult;
import com.reporting.yb.framework.common.pojo.PageParam;
import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;
import com.reporting.yb.framework.excel.core.util.ExcelUtils;
import com.reporting.yb.module.system.controller.admin.reportingfile.vo.ReportingFilePageReqVO;
import com.reporting.yb.module.system.controller.admin.reportingfile.vo.ReportingFileRespVO;
import com.reporting.yb.module.system.controller.admin.reportingfile.vo.ReportingFileSaveReqVO;
import com.reporting.yb.module.system.dal.dataobject.reportingfile.ReportingFileDO;
import com.reporting.yb.module.system.service.reportingfile.ReportingFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.reporting.yb.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.reporting.yb.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 投诉文件")
@RestController
@RequestMapping("/system/reporting-file")
@Validated
public class ReportingFileController {

    @Resource
    private ReportingFileService reportingFileService;

    @PostMapping("/create")
    @Operation(summary = "创建投诉文件")
    @PreAuthorize("@ss.hasPermission('system:reporting-file:create')")
    public CommonResult<String> createReportingFile(@Valid @RequestBody ReportingFileSaveReqVO createReqVO) {
        return success(reportingFileService.createReportingFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉文件")
    @PreAuthorize("@ss.hasPermission('system:reporting-file:update')")
    public CommonResult<Boolean> updateReportingFile(@Valid @RequestBody ReportingFileSaveReqVO updateReqVO) {
        reportingFileService.updateReportingFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:reporting-file:delete')")
    public CommonResult<Boolean> deleteReportingFile(@RequestParam("id") String id) {
        reportingFileService.deleteReportingFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:reporting-file:query')")
    public CommonResult<ReportingFileRespVO> getReportingFile(@RequestParam("id") String id) {
        ReportingFileDO reportingFile = reportingFileService.getReportingFile(id);
        return success(BeanUtils.toBean(reportingFile, ReportingFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投诉文件分页")
    @PreAuthorize("@ss.hasPermission('system:reporting-file:query')")
    public CommonResult<PageResult<ReportingFileRespVO>> getReportingFilePage(@Valid ReportingFilePageReqVO pageReqVO) {
        PageResult<ReportingFileDO> pageResult = reportingFileService.getReportingFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportingFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投诉文件 Excel")
    @PreAuthorize("@ss.hasPermission('system:reporting-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportingFileExcel(@Valid ReportingFilePageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportingFileDO> list = reportingFileService.getReportingFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投诉文件.xls", "数据", ReportingFileRespVO.class,
                BeanUtils.toBean(list, ReportingFileRespVO.class));
    }

}